﻿#if NETCOREAPP3_0
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace Bootstrap.Security.Mvc
{
    public class MvcStartup
    {
        public MvcStartup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddConfigurationManager();
            services.AddBootstrapAdminAuthentication(Configuration);
            var menus = new List<BootstrapMenu>() { new BootstrapMenu() { Id = "2", ParentId = "1", IsResource = 2, Url = "put" }, new BootstrapMenu() { Id = "1", ParentId = "0", Url = "/api/Test", IsResource = 0, } };
            services.AddButtonAuthorization((userName, url, auths) =>
            {
                return true;
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseAuthentication();
            app.UseWhen(context => context.Request.Path.StartsWithSegments("/Home/Index"), builder =>
            {
                builder.Use(async (context, next) =>
                {
                    var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                    identity.AddClaim(new Claim(ClaimTypes.Name, "Unit Test"));
                    context.User = new ClaimsPrincipal(identity);
                    await next();
                });
            });
            app.UseWhen(context => context.Request.Path.StartsWithSegments("/Account/AccessDenied"), builder =>
            {
                var buffer = Encoding.UTF8.GetBytes("Access Denied");
                builder.Run(context => Task.Run(() => context.Response.Body.Write(buffer, 0, buffer.Length)));
            });
            app.UseWhen(context => context.Request.Path.StartsWithSegments("/api/Test") && (context.Request.Method == "POST" || context.Request.Method == "PUT"), builder => builder.Use(async (context, next) =>
            {
                //TestHelper.AuthorizateLicense(typeof(BootstrapAdminAuthorizeFilter));
                await next();
            }));
            app.UseBootstrapAdminAuthentication("", RetrieveRolesByUserName, RetrieveRolesByUrl, RetrieveAppsByUserName);
            app.UseEndpoints(endpoints => endpoints.MapDefaultControllerRoute());
        }

        private IEnumerable<string> RetrieveRolesByUserName(string userName)
        {
            return new string[] { "Administrators", "Default" };
        }

        private IEnumerable<string> RetrieveRolesByUrl(string userName)
        {
            return new string[] { "Administrators", "Default" };
        }

        private IEnumerable<string> RetrieveAppsByUserName(string userName)
        {
            return new string[] { "2", "4" };
        }
    }
}
#endif
